package com.coolw.spring.statemachine.support.order;

import com.coolw.spring.statemachine.constant.StateMachineConst;
import com.coolw.spring.statemachine.enums.OrderEvent;
import com.coolw.spring.statemachine.enums.OrderStatusEnum;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 状态机执行异常处理
 *
 * @author coolw
 * @date 2024/4/16 14:11
 */
@Component
public class OrderStateMachineErrorAction implements Action<OrderStatusEnum, OrderEvent> {

    @Override
    public void execute(StateContext<OrderStatusEnum, OrderEvent> context) {
        Exception ex = context.getException();
        Map<Object, Object> varMap = context.getExtendedState().getVariables();
        varMap.put(StateMachineConst.HAS_ERROR, true);
        varMap.put(StateMachineConst.ERROR, ex);
        varMap.put(StateMachineConst.MESSAGE, ex.getMessage());
    }
}
